package site.marqstree.kotlin.library.base.data.net.convert

import io.reactivex.Observable
import io.reactivex.functions.Function
import site.marqstree.kotlin.library.base.config.constant.ResultCode
import site.marqstree.kotlin.library.base.data.bean.response.BaseResp
import site.marqstree.kotlin.library.base.data.net.exception.RespException


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.baselibrary.rx
 * 文件名: BaseFuncBoolean
 * 创建者: marqstree
 * 创建时间: 2020/2/20 19:23
 * 描述: TODO
 */
/*
    Boolean类型转换封装
 */
class BaseFuncBoolean<T>: Function<BaseResp<T>, Observable<Boolean>> {
    override fun apply(t: BaseResp<T>): Observable<Boolean> {
        //网络响应代码不成功时，触发Rx异常
        if (t.code != ResultCode.SUCCESS){
            return Observable.error(
                RespException(
                    t.code,
                    t.message
                )
            )
        }
        //网络响应码为成功时，
        // 返回将Observable<String>转为Observable<Boolean>
        return Observable.just(true)
    }
}